package button;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonTest2 {
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            var frame = new ButtonFrame();
            frame.setTitle("ButtonTest");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

class ButtonFrame extends JFrame {
    private JPanel buttonPanel;

    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 200;

    public ButtonFrame() {
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        buttonPanel = new JPanel();
        makeButton("Red", Color.RED);
        makeButton("Green", Color.GREEN);
        makeButton("Blue", Color.BLUE);
        add(buttonPanel);
    }

    public void makeButton(String name, Color backgroundColor) {
        var button = new JButton(name);
        buttonPanel.add(button);
        button.addActionListener(_ -> buttonPanel.setBackground(backgroundColor));
    }

}


